﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TP2
{
    public partial class BankClientForm : Form
    {
        public BankClientForm()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddBankForm addForm = new AddBankForm(this);
            addForm.Show();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                string title = dataGridView1[0, index].Value.ToString();
                string bik = dataGridView1[1, index].Value.ToString();
                string inn = dataGridView1[2, index].Value.ToString();
                long capital = Convert.ToInt64(dataGridView1[3, index].Value);
                string type = dataGridView1[4, index].Value.ToString();
                string founders = dataGridView1[5, index].Value.ToString();
                int id_client = Convert.ToInt32(dataGridView1[6, index].Value);
                EditBankForm editBank = new EditBankForm(id_client, title, bik, inn, capital, type, founders, this);
                editBank.Show();
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void RefresTable()
        {
            BankTypeClass selMass = new BankTypeClass();
            DBWorkClass dbWork = new DBWorkClass();
            selMass = dbWork.SelectFromBank();
            if (selMass.id_bank.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
                dataGridView1[3, 0].Value = "";
                dataGridView1[4, 0].Value = "";
                dataGridView1[5, 0].Value = "";
                dataGridView1[6, 0].Value = "";
            }
            else { dataGridView1.RowCount = selMass.id_bank.Count; }
            int j = 0;
            while (j < selMass.id_bank.Count)
            {
                dataGridView1[0, j].Value = selMass.title[j].ToString();
                dataGridView1[1, j].Value = selMass.bik[j].ToString();
                dataGridView1[2, j].Value = selMass.inn[j].ToString();
                dataGridView1[3, j].Value = selMass.capital[j].ToString();
                dataGridView1[4, j].Value = selMass.type[j].ToString();
                dataGridView1[5, j].Value = selMass.founder[j].ToString();
                dataGridView1[6, j].Value = selMass.id_bank[j].ToString();
                j++;
            }
        }

        private void BankClientForm_Load(object sender, EventArgs e)
        {
            this.RefresTable();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int idBank = Convert.ToInt32(dataGridView1[6, index].Value);
                DBWorkClass dbWork = new DBWorkClass();
                dbWork.DeleteFromBank(idBank);
            }
            catch (FormatException) { MessageBox.Show("ошибка накосячили вы товарищь"); }
            this.RefresTable();
        }

        private void buttonView_Click(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                int idBank = Convert.ToInt32(dataGridView1[6, index].Value);
                if (comboBox1.SelectedIndex == 0)
                {
                    LoanForm loanFr = new LoanForm(idBank, "банк");
                    loanFr.Show();
                }
                if (comboBox1.SelectedIndex == 1)
                {
                    CorrespondentForm corrForm = new CorrespondentForm(idBank);
                    corrForm.Show();
                }
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            buttonView.Enabled = true;
        }
    }
}
